/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.usercenter.application.action.dept;

import cn.hallele.infrastructure.metadata.exception.children.ActionException;
import cn.hallele.infrastructure.metadata.io.Page;
import cn.hallele.infrastructure.util.DateUtil;
import cn.hallele.infrastructure.util.ObjectUtil;
import cn.hallele.mc.sdk.usercenter.common.enums.DeptStatusEnum;
import cn.hallele.ms.usercenter.application.bo.DeptBO;
import cn.hallele.ms.usercenter.application.bo.UserBO;
import cn.hallele.ms.usercenter.application.das.DeptBDDas;
import cn.hallele.ms.usercenter.application.das.UserBDDas;
import cn.hallele.ms.usercenter.integrartion.common.constants.USCMsgCodeConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * hallele_ms_usercenter
 *
 * @author anle5
 * @since 2024/11/16 16:22
 */
@Slf4j
@Service
public class DeptApplicationService {
    @Autowired
    private DeptBDDas deptBDDas;

    @Autowired
    private UserBDDas userBDDas;

    @Autowired
    private MessageSource i18n;

    /**
     * 分页查询所有部门名
     *
     * @param pageNum   页码
     * @param pageSize  每页数量
     * @param condition 查询条件
     * @return 分页结果
     */
    public Page<DeptBO> findAll(int pageNum, int pageSize, DeptBO condition) {
        Page<DeptBO> result = deptBDDas.findAll(pageNum, pageSize, condition);
        if (result.getTotalNum() <= 0) {
            return result;
        }

        Set<Long> userIds = result.getData().stream().map(d -> d.getLeader().getId()).collect(Collectors.toSet());
        List<UserBO> byIdIn = userBDDas.findByIdIn(new ArrayList<>(userIds));
        if (ObjectUtil.isEmpty(byIdIn)) {
            return result;
        }

        Map<Long, String> userMap = byIdIn.stream().collect(Collectors.toMap(UserBO::getId, UserBO::getUsername));
        result.getData().forEach(d -> d.getLeader().setUsername(userMap.get(d.getLeader().getId())));
        return result;
    }

    /**
     * 查询单个部门信息
     *
     * @param condition 查询条件
     * @return 部门信息
     */
    public DeptBO findOne(DeptBO condition) {
        DeptBO result = deptBDDas.findOne(condition);
        UserBO user = userBDDas.findById(result.getLeader().getId());
        result.getLeader().setUsername(user.getUsername());
        return result;
    }

    /**
     * 新增 部门
     *
     * @param deptBO 部门
     */
    public void addOne(DeptBO deptBO) {
        DeptBO.Leader leader = deptBO.getLeader();
        UserBO user = userBDDas.getUser(leader.getUsername());
        leader.setId(user.getId());
        deptBO.setCreateTime(DateUtil.nowDate());
        deptBO.setUpdateTime(DateUtil.nowDate());
        deptBDDas.addOne(deptBO);
    }

    /**
     * 更新 部门
     *
     * @param deptBO 部门
     */
    public void updateOne(DeptBO deptBO) {
        Integer number = deptBO.getNumber();
        Integer parentId = deptBO.getParentId();
        if (parentId.equals(number)) {
            log.error("Dept parent can not be itself.");
            throw new ActionException(USCMsgCodeConst.DEPT_CASCADE_ERROR, "Dept parent can not be itself or it's children.");
        }

        DeptBO.Leader leader = deptBO.getLeader();
        UserBO user = userBDDas.getUser(leader.getUsername());
        leader.setId(user.getId());
        deptBDDas.updateOne(deptBO);
    }

    /**
     * 删除 部门
     *
     * @param id 要删除的dept id
     */
    public void delete(Integer id) {
        deptBDDas.delete(id);
    }

    /**
     * 更新 部门 状态
     *
     * @param id         主键
     * @param deptStatus 状态
     */
    public void updateDeptStatus(Integer id, DeptStatusEnum deptStatus) {
        deptBDDas.updateDeptStatus(id, deptStatus);
    }
}
